package com.cqtczh.qhd.driver.acticity;

import httptools.HttpJsonTool;

import java.io.File;
import java.io.IOException;

import tools.L;

import javabeen.ReceiveOrderMessageBeen;
import javabeen.RegisterBeen;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.utils.OrderSoundRecorder;

import android.app.Activity;
import android.app.Notification;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author 作者 E-mail:
 * @date 创建时间：2015-5-30 下午1:10:57
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class RecorderSound extends Activity {
	OrderSoundRecorder orderSoundRecorder;
	HttpJsonTool httpJsonTool;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.delete_activity_recordersound);
		
		httpJsonTool = new HttpJsonTool(this);
		// 录音
		Button button = (Button) findViewById(R.id.button1);
		// 播放
		Button button2 = (Button) findViewById(R.id.button2);
		// 上传
		Button button3 = (Button) findViewById(R.id.button3);

		orderSoundRecorder = new OrderSoundRecorder(this);
		L.v("开始", "播放声音完成");

		button.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent ev) {
				// TODO Auto-generated method stub
				if (ev.getAction() == MotionEvent.ACTION_DOWN) {
					RecorderSoundStartWork(true);
					L.v("触摸", "down");
				}
				if (ev.getAction() == MotionEvent.ACTION_UP) {
					RecorderSoundStartWork(false);
					L.v("触摸", "up");
				}
				return false;
			}
		});
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MediaPlayer mPlayer = new MediaPlayer();
				try {
					if (orderSoundRecorder.GetSonunFileName() == null) {
						L.v("orderSoundRecorder", "为空");
					} else {
						L.v("orderSoundRecorder",
								orderSoundRecorder.GetSonunFileName());
					}
					mPlayer.setDataSource(orderSoundRecorder.GetSonunFileName());
					mPlayer.prepare();
					mPlayer.start();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});

		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ReceiveOrderMessageBeen receiveOrderMessageBeen = new ReceiveOrderMessageBeen();
			
				
				String url;
				
					url="http://192.168.1.113:8080/cqtczh/fleets/sendMsgWithFile";
					receiveOrderMessageBeen.setOrderId("11111111111111");
					receiveOrderMessageBeen.setType("1");
					receiveOrderMessageBeen.setReceiver("2");
					receiveOrderMessageBeen.setSender("22222222");
					receiveOrderMessageBeen.setSendertype("1");
					receiveOrderMessageBeen.setReceivertype("2");
					receiveOrderMessageBeen.setContent("老板多加200块钱我就送!!!");
					HTTPStartwork(url,receiveOrderMessageBeen,orderSoundRecorder.GetSonunFileName());
				
//				}else {
//				///	File file= 
//					receiveOrderMessageBeen.setFileName("");
//					url="http://192.168.1.113:8080/cqtczh/fleets/sendMsgNoFile";
//					receiveOrderMessageBeen.setOrderId("11111111111111");
//					receiveOrderMessageBeen.setType("1");
//					receiveOrderMessageBeen.setReceiver("2");
//					receiveOrderMessageBeen.setSender("22222222");
//					receiveOrderMessageBeen.setSendertype("1");
//					receiveOrderMessageBeen.setReceivertype("2");
//					receiveOrderMessageBeen.setContent("老板多加200块钱我就送!!!");
//					HTTPStartwork(url,receiveOrderMessageBeen,"");
//				}
			
		
			}
		});

		// 获得验证码
		Button button4 = (Button) findViewById(R.id.button4);
		button4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				register();
			}
		});

		final EditText editText1 = (EditText) findViewById(R.id.editText1);

		// 上传验证码登录
		Button button5 = (Button) findViewById(R.id.button5);
		button5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				RegisterBeen registerBeen = new RegisterBeen();
				httpJsonTool.SetOnLister(new Handler() {
					@Override
					public void handleMessage(Message msg) {
						// TODO Auto-generated method stub
						super.handleMessage(msg);
						if (msg.what == 1) {
							// 上传成功
							Toast.makeText(RecorderSound.this, "操作成功",
									Toast.LENGTH_LONG).show();
						}
					}
				});
				registerBeen.setPassword("123");
				registerBeen.setPhone("13330227101");
				registerBeen.setType("1");
				registerBeen.setValidateCode(editText1.getText().toString());
				httpJsonTool.StartWork(registerBeen,
						"http://192.168.1.121:80/cqtczh/fleets/register.do",
						null, null);
			}

		});
	}

	private void register() {

		RegisterBeen registerBeen = new RegisterBeen();
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					// 上传成功
					Toast.makeText(RecorderSound.this, "操作成功",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		registerBeen.setPassword("123");
		registerBeen.setPhone("13330227101");
		registerBeen.setType("1");

		httpJsonTool.StartWork(registerBeen,
				"http://192.168.1.121:80/cqtczh/fleets/getValidateCode.do",
				null, null);
	}

	private void HTTPStartwork(String url,ReceiveOrderMessageBeen data,String filename) {
		httpJsonTool.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					// 上传成功
					Toast.makeText(RecorderSound.this, "操作成功",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		if (filename=="") {
			httpJsonTool.StartWork(data,
					url, null,
					null);
		}
	//	httpJsonTool.StartWork(data,url, null,new File(orderSoundRecorder.GetSonunFileName()));

	}

	private void RecorderSoundStartWork(boolean flag) {
		if (flag) {
			// TODO Auto-generated method stub

			orderSoundRecorder.StartRecorder();

		} else if (orderSoundRecorder != null) {

			orderSoundRecorder.StopRecorder();
		} else {
			Toast.makeText(this, "播放器为空", Toast.LENGTH_LONG).show();
		}

	}

	
	
}
